package com.feonix.common.view.menu;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.feonix.common.util.StringUtil;
import com.feonix.common.view.menu.MenuItem;
import com.feonix.common.permission.PermissionStructure;

public class MenuItem extends PermissionStructure {
	private String id = null;

	private String pid = null;

	private String name = null;

	private String clazz = null;

	private String style = null;

	private String channel = null;

	private String page = null;

	private String tree = null;

	private boolean separator = false;

	private String onClick = null;

	private List<MenuItem> subMenuItems = new ArrayList<MenuItem>();

	public static MenuItem getInstance(String id, String pid, String name) {
		return new MenuItem().setId(id).setPid(pid).setName(name);
	}

	public static MenuItem getInstance(String id, String pid, String name,
			String page) {
		return getInstance(id, pid, name).setPage(page);
	}

	public static MenuItem getInstance(String id, String pid, String name,
			String tree, String page) {
		return getInstance(id, pid, name, page).setTree(tree);
	}

	public String getId() {
		return this.id;
	}

	public MenuItem setId(String id) {
		this.id = id;
		return this;
	}

	public String getName() {
		return this.name;
	}

	public MenuItem setName(String name) {
		this.name = name;
		return this;
	}

	public String getClazz() {
		return this.clazz;
	}

	public MenuItem setClazz(String clazz) {
		this.clazz = clazz;
		return this;
	}

	public String getChannel() {
		return this.channel;
	}

	public MenuItem setChannel(String channel) {
		this.channel = channel;
		return this;
	}

	public String getPage() {
		return this.page;
	}

	public MenuItem setPage(String page) {
		this.page = page;
		return this;
	}

	public String getTree() {
		return this.tree;
	}

	public MenuItem setTree(String tree) {
		this.tree = tree;
		return this;
	}

	public boolean isSeparator() {
		return this.separator;
	}

	public MenuItem setSeparator(boolean separator) {
		this.separator = separator;
		return this;
	}

	MenuItem addSubMenuItem(List<MenuItem> subMenuItems) {
		if (subMenuItems != null) {
			this.subMenuItems.addAll(subMenuItems);
		}
		return this;
	}

	public List<MenuItem> getSubMenuItems() {
		return this.subMenuItems;
	}

	public String getOnClick() {
		return this.onClick;
	}

	public MenuItem setOnClick(String onClick) {
		this.onClick = onClick;
		return this;
	}

	public String getStyle() {
		return this.style;
	}

	public MenuItem setStyle(String style) {
		this.style = style;
		return this;
	}

	public String getPid() {
		return this.pid;
	}

	public MenuItem setPid(String pid) {
		this.pid = pid;
		return this;
	}

	public MenuItem setPermission(String moduleId, String functionId) {
		setModuleId(moduleId);
		setFunctionId(functionId);
		return this;
	}

	public MenuItem setPermission(String moduleId, String functionId, String params) {
		setModuleId(moduleId);
		setFunctionId(functionId);
		setParams(params);
		return this;
	}

	public MenuItem setAllowed(boolean allowedAdmin, boolean allowedGroup) {
		setAllowedAdmin(Boolean.valueOf(allowedAdmin));
		setAllowedGroup(Boolean.valueOf(allowedGroup));
		return this;
	}

	public boolean isAutoHide() {
		boolean bl = false;
		if ((StringUtil.isEmpty(this.page)) && (StringUtil.isEmpty(this.tree)) && (StringUtil.isEmpty(this.onClick))
				&& (CollectionUtils.isEmpty(this.subMenuItems))) {
			bl = true;
		}
		return bl;
	}
}
